package com.example.blog.web;

import com.example.blog.annotation.LoginUser;
import com.example.blog.annotation.RequiresPermission;
import com.example.blog.dao.PostClassInfoRepository;
import com.example.blog.dao.PostsInfoRepository;
import com.example.blog.entity.PostsInfo;
import com.example.blog.exception.NewWifiLoginException;
import com.example.blog.util.Result;
import com.example.blog.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.persistence.*;
import java.util.Date;
import java.util.Map;
import java.util.Optional;

import static com.example.blog.constant.ApiUrlConstants.*;

/**
 * @ClassName: comments
 * @Description: 帖子评论

 * @Date 2023/9/9
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/v1")
@Api(value = "帖子管理",description = "帖子管理")
public class PostInfoController {
    @Resource
    PostsInfoRepository postsInfoRepository;
    @Resource
    PostClassInfoRepository postClassInfoRepository;

    /**
     * 新增帖子
     * @param userId
     * @param postsInfo
     * @return
     */
    @PostMapping("/post")
    @ApiOperation(value = "帖子-新增")
    @RequiresPermission(roles = {ADMIN,USER})
    public Result addPost(@LoginUser String userId,@RequestBody PostsInfo postsInfo){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        postsInfo.setCreUser(userId);
        postsInfo.setUserId(userId);
        postsInfo.setCreTime(new Date());
        postsInfo.setUpdateTime(new Date());
        postsInfo.setUpdUser(userId);
        PostsInfo save = postsInfoRepository.save(postsInfo);
        return Result.ok().setData(save);
    }

    /**
     * 查看帖子
     * @param userId
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiOperation(value = "帖子-列表")
    @GetMapping("/post")
    @RequiresPermission(roles = {ADMIN,USER,GUEST})
    public Result getPost(@LoginUser String userId, @RequestParam Integer pageSize, @RequestParam Integer pageNum){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        pageNum = pageNum == null || pageNum < 0 ? 0:pageNum-1;
        Pageable pageable = PageRequest.of(pageNum,pageSize); //页码：前端从1开始，jpa从0开始，做个转换
        Page<Map<String, Object>> byUset = postsInfoRepository.selectList(pageable);
        return Result.ok().setData(byUset);
    }
    @ApiOperation(value = "帖子-删除")
    @DeleteMapping("/post/{id}")
    @RequiresPermission(roles = {ADMIN,USER})
    public Result delPost(@LoginUser String userId,@PathVariable String id){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        postsInfoRepository.deleteById(id);
        return Result.ok();
    }
    @ApiOperation(value = "帖子-详情")
    @PostMapping("/post/{id}")
    @RequiresPermission(roles = {ADMIN,USER,GUEST})
    public Result getPost(@LoginUser String userId,@PathVariable String id){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        Optional<PostsInfo> byId = postsInfoRepository.findById(id);
        if (byId.isPresent()) {
            return Result.ok().setData(byId.get());
        }
        return Result.failure("该帖子不存在");
    }
    @ApiOperation(value = "帖子-更新")
    @PutMapping("/post")
    @RequiresPermission(roles = {ADMIN,USER})
    public Result uptPost(@LoginUser String userId,@RequestBody PostsInfo postsInfo){
        if (StringUtils.isEmpty(userId)) {
            throw  new NewWifiLoginException("未登录");
        }
        PostsInfo save = postsInfoRepository.save(postsInfo);
        return Result.ok().setData(save);
    }

}
